<template>
  <div class="notes_tree">
    <div class="tree_title">自定义注解</div>
    <el-tree :data="list" default-expand-all node-key="id">
      <!-- :render-content="renderContent" -->
      <!-- data.isKey代表键值的输入框和子注释的输入框 两种不同类型 -->
      <span slot-scope="{ node, data }">
        <div v-if="data.isKey">
          <div>
            <span style="margin-right: 6px">-</span>
            <el-input
              @click.stop.native=""
              style="width: 105px; margin-right: 6px"
              type="text"
              size="mini"
              class="Inputbox"
              v-model="data.label"
            >
              <template slot="prepend">键</template>
            </el-input>
            <el-input
              @click.stop.native=""
              style="width: 105px"
              type="text"
              size="mini"
              class="Inputbox"
              v-model="data.value"
            >
              <template slot="prepend">值</template>
            </el-input>
            <i
              style="margin-left: 6px"
              @click.stop="remove(node, data)"
              class="el-icon-delete"
            />
          </div>
        </div>
        <div class="buildInput" v-else>
          <span class="CommentName" style="margin-right: 7px">{{
            node.level == 1 ? "注释名：" : "-"
          }}</span>
          <el-input
            @click.stop.native=""
            style="width: 216px"
            type="text"
            size="mini"
            class="Inputbox"
            v-model="data.label"
          >
            <template slot="prepend">
              <svg-icon icon-class="aite" style="font-size: 16px"></svg-icon>
            </template>
          </el-input>
          <el-dropdown style="margin-left: 3px">
            <span class="el-dropdown-link">
              <svg-icon
                @click.stop.native=""
                icon-class="more"
                class="svgmore"
              ></svg-icon>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div @click.stop="append(data)">添加子注释</div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div @click.stop="addKeyValue(data)">添加键值</div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div @click.stop="remove(node, data)">
                  <i class="el-icon-delete" />
                  删除
                </div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </span>
    </el-tree>
  </div>
</template>

<script>
export default {
  name: "NotesTree",
  props: {
    list: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      //自定义树形菜单key
      // defaultProps: {
      //   children: "children",
      //   label: "label",
      // },
    };
  },
  mounted() {},
  methods: {
    //添加节点
    append(data) {
      this.$emit("addSubComment", data);
    },
    //删除节点
    remove(node, data) {
      this.$emit("remove", node, data);
    },
    //添加键值 可以和添加节点优化成一个 方便测试 后期在改
    addKeyValue(data) {
      this.$emit("addKeyValue", data);
    },
    //自定义树形结构dom节点
    renderContent(h, { node, data, store }) {
      // isKey 代表是 键值 渲染键值的dom
      if (data.isKey) {
        return (
          <div>
            <div>
              <el-input
                style="width: 96px;margin-right: 6px;"
                type="text"
                size="mini"
                class="Inputbox"
                v-model={data.label}
              >
                <template slot="prepend">键</template>
              </el-input>
              <el-input
                style="width: 96px;margin-right: 6px;"
                type="text"
                size="mini"
                class="Inputbox"
                v-model={data.value}
              >
                <template slot="prepend">值</template>
              </el-input>
              <i
                class="el-icon-delete"
                on-click={() => this.remove(node, data)}
              />
            </div>
          </div>
        );
      } else {
        return (
          <div class="buildInput">
            <span class="CommentName">{node.level == 1 ? "注释名：" : ""}</span>
            <el-input
              on-click={(e) => {
                e.stopPropagation();
              }}
              style="width: 200px;"
              type="text"
              size="mini"
              class="Inputbox"
              v-model={data.label}
            >
              <template slot="prepend">
                <svg-icon icon-class="aite" style="font-size: 16px"></svg-icon>
              </template>
            </el-input>
            <el-dropdown>
              <span class="el-dropdown-link">
                <svg-icon icon-class="more" class="svgmore"></svg-icon>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <div on-click={() => this.append(data)}>添加子注释</div>
                </el-dropdown-item>
                <el-dropdown-item>
                  <div on-click={() => this.addKeyValue(data)}>添加键值</div>
                </el-dropdown-item>
                <el-dropdown-item>
                  <div on-click={() => this.remove(node, data)}>
                    <i class="el-icon-delete" />
                    删除
                  </div>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        );
      }
    },
  },
};
</script>

<style lang='scss'>
.tree_title {
  /* font-size: #666; */
  font-size: 14px;
  margin-top: 20px;
}
.notes_tree {
  overflow-x: auto;
  .CommentName {
    font-size: 14px;
    color: #666;
  }
}
/* .notes_tree .el-tree { */
/* padding-top: 15px; */
/* background-color: rgba(233, 233, 233, 0.3); */
/* } */
.notes_tree .el-tree-node > .el-tree-node__children {
  overflow: inherit;
}
.notes_tree .el-tree-node__children .el-tree-node__content {
  margin-left: 30px !important;
}
/* 取消鼠标经过背景色 */
.notes_tree .el-tree-node__content:hover {
  background-color: transparent;
}
.notes_tree .el-tree-node {
  margin: 20px 0;
}
</style>
